<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "mall_order".
 *
 * @property int $id
 * @property int $mid 商家用户id
 * @property int $uid 下单用户id
 * @property string $openid 用户openid
 * @property string $out_trade_no 订单号
 * @property string $total_fee 总费用
 * @property string $amount 商品总费用
 * @property string $logistic_fee 物流费用
 * @property string $name 收货人姓名
 * @property string $phone 收货人电话
 * @property string $address 收货人电话
 * @property string $logistics 物流公司名
 * @property string $order_num 物流订单号
 * @property int $create_time 创建时间
 * @property int $send_time 发货时间
 * @property int $status -1未支付 1已支付 2已发货 3已收货
 * @property string $info 订单信息
 * @property string $discount 折扣值
 * @property string $recharge 支票
 */
class MallOrder extends \yii\db\ActiveRecord
{

    public $create_time_start;
    public $create_time_end;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'mall_order';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['mid', 'uid', 'openid', 'out_trade_no', 'total_fee', 'amount', 'logistic_fee', 'name', 'phone', 'address', 'create_time'], 'required'],
            [['mid', 'uid', 'create_time', 'status'], 'integer'],
            [['total_fee', 'amount', 'logistic_fee', 'discount', 'recharge', 'send_time'], 'number'],
            [['create_time_start','create_time_end'], 'string'],
            [['openid', 'out_trade_no', 'address'], 'string', 'max' => 100],
            [['name', 'phone', 'logistics'], 'string', 'max' => 20],
            [['info'], 'string','max' =>1000],
            [['order_num'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'mid' => '商家ID',
            'uid' => '用户ID',
            'openid' => 'Openid',
            'out_trade_no' => '订单号',
            'total_fee' => '总费用',
            'amount' => '商品总费用',
            'logistic_fee' => '快递费用',
            'name' => '收件人',
            'phone' => '电话',
            'address' => '地址',
            'logistics' => '快递公司名',
            'order_num' => '快递单号',
            'create_time' => '时间',
            'status' => '状态',
            'create_time_start' => '开始时间',
            'create_time_end' => '结束时间',
            'discount' => '折扣',
            'recharge' => '支票',
            'send_time' => '发货时间',
        ];
    }

    public static function attributeValue()
    {
        return [
            'status' => [
                '' => '',
                '-1' => '未支付',
                '1' => '已支付',
                '2' => '已发货',
                '3' => '已收货',
                '4' => '未支付删除',
                '5' => '已支付删除',
            ],
        ];
    }

    public function getStatusValue()
    {
        switch ($this->status)
        {
            case -1:
                $value = '未支付';
                break;
            case 1:
                $value = '已支付';
                break;
            case 2:
                $value = '已发货';
                break;
            case 3:
                $value = '已收货';
                break;
            case 4:
                $value = '未支付删除';
                break;
            case 5:
                $value = '已支付删除';
                break ;
            default:
                $value = '未知状态';
                break;
        }
        return $value;
    }

    /**
     * 订单列表
     * @return \yii\db\ActiveQuery
     */
    public function getList()
    {
        return $this->hasMany(MallOrderList::class, ['out_trade_no' => 'out_trade_no']);
    }

    public function getUser()
    {
        return $this->hasOne(EduUser::class, ['id' => 'uid']);
    }

    /**
     * 获取订单详情
     * @param $out_trade_no
     * @return array
     */
    public static function orderList($out_trade_no)
    {
        $sql = "SELECT * FROM mall_order_list WHERE find_in_set('{$out_trade_no}', out_trade_no)";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }

    /**
     * 查询一条产品
     * @param $pid
     * @return array|false
     */
    public static function productOne($pid)
    {
        $sql = "SELECT * FROM mall_product WHERE  id = {$pid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 获取分佣详情
     * @param $out_trade_no
     * @return array
     */
    public static function billList($out_trade_no)
    {
        $sql = "SELECT * FROM mall_bill WHERE find_in_set('{$out_trade_no}', out_trade_no)";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }

    /**
     * 获取一个上级用户的昵称
     * @param $uid
     * @return array|false
     */
    public static function userOne($uid)
    {
        $sql = "SELECT nickname FROM edu_user WHERE id = {$uid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }
}
